//
//  MineViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/11.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

class MineViewController: UIViewController {
    @IBOutlet weak var headImg: UIImageView!
    @IBOutlet weak var nameLbl: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        
        if user.accountId == "" {
            self.performSegue(withIdentifier: "toLogin", sender: nil)
        }
        headImg.layer.cornerRadius = headImg.frame.size.height/2
        headImg.layer.masksToBounds = true
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        nameLbl.text = user.userName
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    @IBAction func doSome(_ sender: UIButton) {
        print(sender.tag)
        switch sender.tag {
        case 1:
           self.performSegue(withIdentifier: "toTimeTable", sender: nil)
        case 2:
            if user.teacherId != "0"{
                self.performSegue(withIdentifier: "toMyTeacher", sender: nil)
            }else{
                showMsg(msg: "您还未分配教练")
            }
        case 3:
            if user.teacherId != "0"{
                self.performSegue(withIdentifier: "toBespeak", sender: nil)
            }else{
                showMsg(msg: "您还未分配教练")
            }
        case 4:
            self.performSegue(withIdentifier: "toSection", sender: nil)
        case 5:
            self.performSegue(withIdentifier: "toMyOrder", sender: nil)
        case 6:
            setPopView()
        default:
            self.performSegue(withIdentifier: "toComplain", sender: nil)
        }
    }
    
    @IBAction func toPersonInfo(_ sender: Any) {
        self.performSegue(withIdentifier: "toPersonInfo", sender: nil)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "toLogin" {
            let dest = segue.destination as! LoginViewController
            dest.fromWhere = "Mine"
        }
    }
    
    func setPopView(){
        let bgView = UIView(frame: CGRect(x: 0, y: 0, width:KMainScreenWidth , height: KMainScreenHeight))
        bgView.backgroundColor = UIColor(red: 18/255.0, green: 18/255.0, blue: 18/255.0, alpha: 0.6)
        self.view.addSubview(bgView)
        bgView.tag = 9
        
        let av = UIView(frame: CGRect(x: 30, y: KMainScreenHeight/3, width: KMainScreenWidth-60, height: KMainScreenHeight/3))
        av.layer.cornerRadius = 15
        av.backgroundColor = UIColor.white
        av.tag = 9
        bgView.addSubview(av)
        
        let lab1 = UILabel(frame: CGRect(x: 10, y: 50, width: av.frame.size.width-20, height: 40))
        lab1.text = "咨询热线：\(KF_PHONE)"
        lab1.font = UIFont.systemFont(ofSize: 24)
        
        let lab2 = UILabel(frame: CGRect(x: 10, y: 91, width: av.frame.size.width-20, height: 40))
        lab2.text = "工作时间：9:00-18:00"
        lab2.font = UIFont.systemFont(ofSize: 24)
        av.addSubview(lab1)
        av.addSubview(lab2)
        
        let btnL = UIButton(frame: CGRect(x: 40, y: av.frame.size.height - 60, width: 90, height: 30))
        
        btnL.setTitle("呼叫", for: .normal)
        btnL.tintColor = UIColor.white
        btnL.backgroundColor = KP_MAIN_BLUE
        btnL.layer.cornerRadius = 5
        btnL.addTarget(self, action: #selector(callPhone), for: UIControlEvents.touchUpInside)
        
        av.addSubview(btnL)
        
        let btnR = UIButton(frame: CGRect(x: av.frame.size.width - 130, y: av.frame.size.height - 60, width: 90, height: 30))
        
        btnR.setTitle("取消", for: .normal)
        btnR.setTitleColor(UIColor.black, for: .normal)
        btnR.layer.borderColor = KP_MAIN_BLUE.cgColor
        btnR.layer.borderWidth = 1
        btnR.layer.cornerRadius = 5
        btnR.addTarget(self, action: #selector(cancelPop), for: UIControlEvents.touchUpInside)
        av.addSubview(btnR)
        
        
        
        
    }
    
    func cancelPop(){
        for x in self.view.subviews{
            if x.tag == 9 {
                x.removeFromSuperview()
            }
        }
    }
    
    func callPhone() {
        print("打电话")
        UIApplication.shared.openURL(NSURL(string: "tel://\(KF_PHONE)")! as URL)
    }

}
